<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <div class="left">欢迎来到叩丁严选商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" width="24px" />
          用户名：{{ userInfo.nickName }}
        </li>
        ``
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" v-else @click="changeIsShowLoginModel(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WeiXinLoginAPI } from "@/request/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.LoginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModel: "LoginModel/changeIsShowLoginModel",
      changeIsLogined: "LoginStatus/changeIsLogined",
      changeUserInfo: "userInfo/changeUserInfo",
    }),
    ...mapActions({
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  async created() {
    //获取code值 (当微信用户点击允许后,页面会重新刷新 ,也就会重新执行topbar中create钩子函数)

    // this.$router.onReady(() => {
    //   console.log("code111:", this.$route.query.code);
    // });
    console.log("进来了topbar-created钩子");
    // console.log("code:", this.$route.query.code);
    let code = this.$route.query.code;
    if (code) {
      //微信扫码登录,找后台去微信换取token
      let res = await WeiXinLoginAPI({
        code,
      });
      console.log("微信登录后res:", res);
      //返回的code中 三种结果
      /*
       效果重现:
       1. [重现407] 微信和手机号没有关联的时候  (去备份地址中解绑)  - 扫码登录
          --- Login.vue 去输入手机号登录 进行关联 
       2. [重现400] 地址栏中有code 并且 code无效就可以了
       3. [重现0]   微信和手机号关联成功后,重新扫码登录
       */
      if (res.code == 0) {
        //1. code==0   登录成功
        //1.1 提示
        alert("扫码登录成功");
        //1.2 存token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        //1.3 更新右上角购物车按钮显示
        this.changeIsLogined(true);
        //1.4 清除地址栏中的code
        this.$route.query.code && this.$router.push(this.$route.path);
      } else if (res.code == 400) {
        //2. code ==400  二维码失效(code失效)
        //2.1 提示重新扫码
        alert("二维码失效,请重新扫码!");
        //2.2 自动打开登录模块框
        this.changeIsShowLoginModel(true);
      } else if (res.code == 407) {
        //3. code ==407  需要用户再进行一次手机号方式登录 (后期让后台将微信和手机号进行关联)
        //3.1 提示用户需要进行一次手机号登录
        alert("请使用手机号再进行登录");
        //3.2 自动打开登录模块框
        this.changeIsShowLoginModel(true);
        //3.3 存储uuid ( 一会登录要用)
        localStorage.setItem("uuid", res.uuid);
      }
    } else {
      //判断是否有token
      let token = localStorage.getItem("x-auth-token");
      this.changeIsLogined(Boolean(token));
      if (token) {
        this.asyncChangeUserInfo(); //更新用户信息 (刷新页面和切换导航)
      } else {
        this.initUserInfo(); //用户信息初始化.vuex (刷新页面和切换导航)
      }
    }
  },
};
</script>
 
<style lang = "less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  color: #fffefe;
  font-family: SourceHanSansSC;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .right {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          background-color: #0a328e;
        }
        img {
          vertical-align: middle;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>